/** @file  create.c
 *  @brief  To create the record
 * 
 *  To Create the record and write into the file
 *   
 *  @author Karthik M 
 *  @bug No  bugs.
 */



#include "header.h"

#define NAMEMAX 100

void create(char *db ,char *line)
{

   FILE *fp ;
   char *name ;
   long  pos;
   int duplicate = 0;

   DATA r1, t1;
#if 0 
   printf ( " the DB = %s \n", db );
#endif

   if ( (fp = fopen(db,"a+")) == NULL){
      fprintf (stderr,"can't open the file \n");
      exit(EXIT_FAILURE); } 

   if ( (name = (char *) malloc(sizeof(char) * NAMEMAX )) == NULL)
   {
      fprintf (stderr," error in creating memory \n");
      exit(EXIT_FAILURE );
   }


   pos = ftell(fp);
redo:
   printf (" Enet the  name :\n");
   strcpy(name, (char *) get_name(line));

   rewind(fp);

   while ( fread(&t1,sizeof(t1),1,fp) ) 
   {
     if ( 0 == (strcmp(name,t1.name))){
        duplicate = 1;
        break;
     }
   }
   if (duplicate == 1){
      printf (" NAME Already EXISITS !!!!!!! Give some nick name \n");
      duplicate = 0;
      goto redo;
   }


   fseek(fp,pos,0);
   
   strcpy(r1.name, name);
   printf ("age:");
   r1.age = get_num(line);
   printf (" Gender  ->1.MALE ->2.FEMALE  :");
   r1.gender = get_enum(line);
   printf (" Status   ->1.SINGLE  ->2.MARRIED :");
   r1.status = get_enum(line);

   print (r1);


   fwrite ( &r1,sizeof(r1),1,fp); 

   free(name);
   fclose(fp);

}
